package com.vanzo.talkie2;

import android.app.Activity;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2018/1/16.
 */

public class FrequencyPicker {

    private static final String TAG = "FrequencyPicker";
    private static List<String> numList1;
    private static List<String> numList2;
    private static List<String> numList3;

    static {
        numList1 = new ArrayList<>();
        numList2 = new ArrayList<>();
        numList3 = new ArrayList<>();
        for (int i = 0; i <= 70; i++) {
            numList1.add(400 + i + "");
        }
        for (int i = 0; i < 10; i++) {
            numList2.add(i + "");
        }
        numList3.add("000");
        numList3.add("125");
        numList3.add("250");
        numList3.add("375");
        numList3.add("500");
        numList3.add("625");
        numList3.add("750");
        numList3.add("875");
    }

    private TriplePicker picker;
    private ColorTextView textView;
    private Activity activity;
    private int rStringId;
    private String defaultValue;

    public FrequencyPicker(Activity activity, ColorTextView textView, int rStringId, String defaultValue) {
        this.activity = activity;
        this.textView = textView;
        this.rStringId = rStringId;
        this.defaultValue = defaultValue;
        this.textView.setTitle(rStringId);
        this.textView.setValue(defaultValue);
        this.textView.setOnClickListener(textViewListener);
    }

    private View.OnClickListener textViewListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (picker == null) {
                picker = new TriplePicker(activity, numList1, numList2, numList3);
                picker.setFirstLabel(null, ".");
                picker.setOnPickListener(new TriplePicker.OnPickListener() {
                    @Override
                    public void onPicked(int var1, int var2, int var3) {
                        textView.setValue(numList1.get(var1) + "." + numList2.get(var2) + numList3.get(var3));
                    }
                });
                picker.setOnWheelListener(new TriplePicker.OnWheelListener() {
                    @Override
                    public void onFirstWheeled(int var1, String var2) {
                        if (var1 == 70) {
                            picker.setSelectedSecondIndex(0);
                            picker.setSelectedThirdIndex(0);
                        }
                    }

                    @Override
                    public void onSecondWheeled(int var1, String var2) {
                        if (var1 != 0 && picker.getSelectedFirstIndex() == 70) {
                            picker.setSelectedFirstIndex(69);
                        }
                    }

                    @Override
                    public void onThirdWheeled(int var1, String var2) {
                        if (var1 != 0 && picker.getSelectedFirstIndex() == 70) {
                            picker.setSelectedFirstIndex(69);
                        }
                    }
                });
            }
            picker.show();
            String defaultValue = textView.getValue().toString();
            picker.setSelectedIndex(numList1.indexOf(defaultValue.substring(0, 3)), numList2.indexOf(defaultValue.substring(4, 5)), numList3.indexOf(defaultValue.substring(5, 8)));
        }
    };
}
